package org.opu.pc.sg;

import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.Map;
import java.util.Properties;
import org.opu.pc.bl.BibliographyLinkService;
import org.opu.pc.domain.domains.Publication;
import org.apache.commons.io.IOUtils;
import org.opu.pc.domain.PublicationManagement;
import org.opu.pc.ui.util.Logger;
import org.opu.pc.util.IOSafeUtils;
import static org.opu.pc.util.StringUtil.*;

/**
 *
 * @author Yuriy Dunko
 */
public class StudentStatementGenerator extends AbstractStatementGenerator {

    private Logger logger = Logger.create(StudentStatementGenerator.class);

    public static enum HeaderType {
        STUDENT("publication.header.students"),
        PROFESSOR("publication.header.professors");

        public final String value;

        private HeaderType(String value) {
            this.value = value;
        }

    }

    public static final String HEADER_TYPE = "headerType";

    public static final String TEMPLATE_PATH = "/org/opu/pc/sg/st2_template.docx";
    public static final String DOCUMENT = "/org/opu/pc/sg/st2_document.xml";
    public static final String LIST = "/org/opu/pc/sg/st2_list.xml";
    public static final String PROPERTIES = "/org/opu/pc/sg/st2_properties.properties";

    private static final String DOCUMENT_TEMPLATE;
    private static final String LIST_TEMPLATE;
    private static final Map<String, String> DOCUMENT_PROPERTIES;

    static {
        try {
            DOCUMENT_TEMPLATE = IOUtils.toString(IOSafeUtils.getResourceAsStream(DOCUMENT));
            LIST_TEMPLATE = IOUtils.toString(IOSafeUtils.getResourceAsStream(LIST));

            Properties p = new Properties();
            p.load(IOSafeUtils.getResourceAsStream(PROPERTIES));
            DOCUMENT_PROPERTIES = Collections.unmodifiableMap(propertiesToMap(p));
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
    }

    protected BibliographyLinkService linkService;
    protected PublicationManagement publicationManagement;

    public StudentStatementGenerator(BibliographyLinkService linkService,
            PublicationManagement publicationManagement){
        this.linkService = linkService;
        this.publicationManagement = publicationManagement;
    }

    @Override
    protected String getDocument() throws IOException {

        StringBuilder publications = new StringBuilder();
        
        Collection<Publication> allPublications = getPublications();
        logger.debug("getDocument: get " + allPublications.size() + " publications.");
        int i = 1;
        for (Publication publication : allPublications) {
            logger.debug("getDocument: publication " + publication.getName() +
                     "[" + i + "] have " + publication.getPublishers().size() +
                     " publishers " + publication.getPublishers());

            String link = linkService.createBibliographyLink(publication);
            publications.append(assignVarValue(LIST_TEMPLATE,
                    toMap("iteration", String.valueOf(i++),
                    "link", prepareBLink(link, publication))));

        }

        sgProperties.putAll(DOCUMENT_PROPERTIES);
        sgProperties.setProperty("head", sgProperties.getProperty(
                ((HeaderType)variables.get(HEADER_TYPE)).value));
        sgProperties.setProperty("year1", String.valueOf(
                sgProperties.getProperty(BEGIN_YEAR_VAR)));

        logger.debug("getDocument: properties: " + sgProperties);
        
        sgProperties.setProperty("list", publications.toString());


        return assignVarValue(DOCUMENT_TEMPLATE, propertiesToMap(sgProperties));
    }

    @Override
    protected String getTemplatePath() {
        return TEMPLATE_PATH;
    }

    protected String prepareBLink(String link, Publication publication){
        return link;
    }

    protected Collection<Publication> getPublications(){
        return publicationManagement.getStudentsPublications(
                (Date)variables.get(BEGIN_YEAR_VAR),
                (Date)variables.get(END_YEAR_VAR));
    }
}
